{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "字段说明\n",
    "  Instant记录号\n",
    "  dteday：日期\n",
    "  season：季节（1=春天、2=夏天、3=秋天、4=冬天）\n",
    "  yr：年份，(0: 2011, 1:2012)\n",
    "  mnth：月份( 1 to 12)\n",
    "  hr：小时 (0 to 23)  （只在hour.csv有，作业忽略此字段）\n",
    "  holiday：是否是节假日\n",
    "  weekday：星期中的哪天，取值为0～6\n",
    "  workingday：是否工作日\n",
    "  1=工作日 （是否为工作日，1为工作日，0为非周末或节假日\n",
    "  weathersit：天气（1：晴天，多云 ",
    "2：雾天，阴天 ",
    "3：小雪，小雨 ",
    "4：大雨，大雪，大雾）\n",
    "  temp：气温摄氏度\n",
    "  atemp：体感温度\n",
    "  hum：湿度\n",
    "  windspeed：风速\n",
    "    \n",
    "  casual：非注册用户个数\n",
    "  registered：注册用户个数\n",
    "  cnt：给定日期（天）时间（每小时）总租车人数，响应变量y （cnt = casual + registered）\n",
    "  casual、registered和cnt三个特征均为要预测的y，作业里只需对cnt进行预测"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>dteday</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>casual</th>\n",
       "      <th>registered</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>2011-01-01</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.344167</td>\n",
       "      <td>0.363625</td>\n",
       "      <td>0.805833</td>\n",
       "      <td>0.160446</td>\n",
       "      <td>331</td>\n",
       "      <td>654</td>\n",
       "      <td>985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>2011-01-02</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0.363478</td>\n",
       "      <td>0.353739</td>\n",
       "      <td>0.696087</td>\n",
       "      <td>0.248539</td>\n",
       "      <td>131</td>\n",
       "      <td>670</td>\n",
       "      <td>801</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2011-01-03</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.196364</td>\n",
       "      <td>0.189405</td>\n",
       "      <td>0.437273</td>\n",
       "      <td>0.248309</td>\n",
       "      <td>120</td>\n",
       "      <td>1229</td>\n",
       "      <td>1349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>2011-01-04</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.200000</td>\n",
       "      <td>0.212122</td>\n",
       "      <td>0.590435</td>\n",
       "      <td>0.160296</td>\n",
       "      <td>108</td>\n",
       "      <td>1454</td>\n",
       "      <td>1562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>2011-01-05</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0.226957</td>\n",
       "      <td>0.229270</td>\n",
       "      <td>0.436957</td>\n",
       "      <td>0.186900</td>\n",
       "      <td>82</td>\n",
       "      <td>1518</td>\n",
       "      <td>1600</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   instant      dteday  season  yr  mnth  holiday  weekday  workingday  \\\n",
       "0        1  2011-01-01       1   0     1        0        6           0   \n",
       "1        2  2011-01-02       1   0     1        0        0           0   \n",
       "2        3  2011-01-03       1   0     1        0        1           1   \n",
       "3        4  2011-01-04       1   0     1        0        2           1   \n",
       "4        5  2011-01-05       1   0     1        0        3           1   \n",
       "\n",
       "   weathersit      temp     atemp       hum  windspeed  casual  registered  \\\n",
       "0           2  0.344167  0.363625  0.805833   0.160446     331         654   \n",
       "1           2  0.363478  0.353739  0.696087   0.248539     131         670   \n",
       "2           1  0.196364  0.189405  0.437273   0.248309     120        1229   \n",
       "3           1  0.200000  0.212122  0.590435   0.160296     108        1454   \n",
       "4           1  0.226957  0.229270  0.436957   0.186900      82        1518   \n",
       "\n",
       "    cnt  \n",
       "0   985  \n",
       "1   801  \n",
       "2  1349  \n",
       "3  1562  \n",
       "4  1600  "
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "data = pd.read_csv(\"day.csv\")\n",
    "data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>instant</th>\n",
       "      <th>season</th>\n",
       "      <th>yr</th>\n",
       "      <th>mnth</th>\n",
       "      <th>holiday</th>\n",
       "      <th>weekday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>weathersit</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>casual</th>\n",
       "      <th>registered</th>\n",
       "      <th>cnt</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "      <td>731.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>366.000000</td>\n",
       "      <td>2.496580</td>\n",
       "      <td>0.500684</td>\n",
       "      <td>6.519836</td>\n",
       "      <td>0.028728</td>\n",
       "      <td>2.997264</td>\n",
       "      <td>0.683995</td>\n",
       "      <td>1.395349</td>\n",
       "      <td>0.495385</td>\n",
       "      <td>0.474354</td>\n",
       "      <td>0.627894</td>\n",
       "      <td>0.190486</td>\n",
       "      <td>848.176471</td>\n",
       "      <td>3656.172367</td>\n",
       "      <td>4504.348837</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>211.165812</td>\n",
       "      <td>1.110807</td>\n",
       "      <td>0.500342</td>\n",
       "      <td>3.451913</td>\n",
       "      <td>0.167155</td>\n",
       "      <td>2.004787</td>\n",
       "      <td>0.465233</td>\n",
       "      <td>0.544894</td>\n",
       "      <td>0.183051</td>\n",
       "      <td>0.162961</td>\n",
       "      <td>0.142429</td>\n",
       "      <td>0.077498</td>\n",
       "      <td>686.622488</td>\n",
       "      <td>1560.256377</td>\n",
       "      <td>1937.211452</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.059130</td>\n",
       "      <td>0.079070</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.022392</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>22.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>183.500000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.337083</td>\n",
       "      <td>0.337842</td>\n",
       "      <td>0.520000</td>\n",
       "      <td>0.134950</td>\n",
       "      <td>315.500000</td>\n",
       "      <td>2497.000000</td>\n",
       "      <td>3152.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>366.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>7.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.498333</td>\n",
       "      <td>0.486733</td>\n",
       "      <td>0.626667</td>\n",
       "      <td>0.180975</td>\n",
       "      <td>713.000000</td>\n",
       "      <td>3662.000000</td>\n",
       "      <td>4548.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>548.500000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>10.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>5.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>2.000000</td>\n",
       "      <td>0.655417</td>\n",
       "      <td>0.608602</td>\n",
       "      <td>0.730209</td>\n",
       "      <td>0.233214</td>\n",
       "      <td>1096.000000</td>\n",
       "      <td>4776.500000</td>\n",
       "      <td>5956.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>731.000000</td>\n",
       "      <td>4.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>12.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>6.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>3.000000</td>\n",
       "      <td>0.861667</td>\n",
       "      <td>0.840896</td>\n",
       "      <td>0.972500</td>\n",
       "      <td>0.507463</td>\n",
       "      <td>3410.000000</td>\n",
       "      <td>6946.000000</td>\n",
       "      <td>8714.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          instant      season          yr        mnth     holiday     weekday  \\\n",
       "count  731.000000  731.000000  731.000000  731.000000  731.000000  731.000000   \n",
       "mean   366.000000    2.496580    0.500684    6.519836    0.028728    2.997264   \n",
       "std    211.165812    1.110807    0.500342    3.451913    0.167155    2.004787   \n",
       "min      1.000000    1.000000    0.000000    1.000000    0.000000    0.000000   \n",
       "25%    183.500000    2.000000    0.000000    4.000000    0.000000    1.000000   \n",
       "50%    366.000000    3.000000    1.000000    7.000000    0.000000    3.000000   \n",
       "75%    548.500000    3.000000    1.000000   10.000000    0.000000    5.000000   \n",
       "max    731.000000    4.000000    1.000000   12.000000    1.000000    6.000000   \n",
       "\n",
       "       workingday  weathersit        temp       atemp         hum   windspeed  \\\n",
       "count  731.000000  731.000000  731.000000  731.000000  731.000000  731.000000   \n",
       "mean     0.683995    1.395349    0.495385    0.474354    0.627894    0.190486   \n",
       "std      0.465233    0.544894    0.183051    0.162961    0.142429    0.077498   \n",
       "min      0.000000    1.000000    0.059130    0.079070    0.000000    0.022392   \n",
       "25%      0.000000    1.000000    0.337083    0.337842    0.520000    0.134950   \n",
       "50%      1.000000    1.000000    0.498333    0.486733    0.626667    0.180975   \n",
       "75%      1.000000    2.000000    0.655417    0.608602    0.730209    0.233214   \n",
       "max      1.000000    3.000000    0.861667    0.840896    0.972500    0.507463   \n",
       "\n",
       "            casual   registered          cnt  \n",
       "count   731.000000   731.000000   731.000000  \n",
       "mean    848.176471  3656.172367  4504.348837  \n",
       "std     686.622488  1560.256377  1937.211452  \n",
       "min       2.000000    20.000000    22.000000  \n",
       "25%     315.500000  2497.000000  3152.000000  \n",
       "50%     713.000000  3662.000000  4548.000000  \n",
       "75%    1096.000000  4776.500000  5956.000000  \n",
       "max    3410.000000  6946.000000  8714.000000  "
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 731 entries, 0 to 730\n",
      "Data columns (total 13 columns):\n",
      "instant       731 non-null float64\n",
      "season        731 non-null float64\n",
      "yr            731 non-null float64\n",
      "mnth          731 non-null float64\n",
      "holiday       731 non-null float64\n",
      "weekday       731 non-null float64\n",
      "workingday    731 non-null float64\n",
      "weathersit    731 non-null float64\n",
      "temp          731 non-null float64\n",
      "atemp         731 non-null float64\n",
      "hum           731 non-null float64\n",
      "windspeed     731 non-null float64\n",
      "cnt           731 non-null float64\n",
      "dtypes: float64(13)\n",
      "memory usage: 74.3 KB\n"
     ]
    }
   ],
   "source": [
    "data = data.drop('dteday',axis=1).drop('registered',axis=1).drop('casual',axis=1)\n",
    "data['instant'] = data['instant'].astype(np.float64)\n",
    "data['season'] = data['season'].astype(np.float64)\n",
    "data['yr'] = data['yr'].astype(np.float64)\n",
    "data['mnth'] = data['mnth'].astype(np.float64)\n",
    "data['holiday'] = data['holiday'].astype(np.float64)\n",
    "data['weekday'] = data['weekday'].astype(np.float64)\n",
    "data['workingday'] = data['workingday'].astype(np.float64)\n",
    "data['weathersit'] = data['weathersit'].astype(np.float64)\n",
    "data['cnt'] = data['cnt'].astype(np.float64)\n",
    "data.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "instant       0\n",
       "season        0\n",
       "yr            0\n",
       "mnth          0\n",
       "holiday       0\n",
       "weekday       0\n",
       "workingday    0\n",
       "weathersit    0\n",
       "temp          0\n",
       "atemp         0\n",
       "hum           0\n",
       "windspeed     0\n",
       "cnt           0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(731, 13)"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(731, 12)\n"
     ]
    }
   ],
   "source": [
    "y = data.cnt.values\n",
    "# 默认删除行，列需要加axis = 1\n",
    "X = data.drop('cnt', axis = 1)\n",
    "print(X.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "from numpy import float64\n",
    " \n",
    "# 随机采样20%的数据构建测试样本，其余作为训练样本\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=33, test_size=0.2)\n",
    "\n",
    "# 数据标准化\n",
    "from sklearn.preprocessing import StandardScaler\n",
    " \n",
    "# 分别初始化对特征和目标值的标准化器\n",
    "ss_X = StandardScaler()\n",
    "ss_y = StandardScaler()\n",
    " \n",
    "# 分别对训练和测试数据的特征以及目标值进行标准化处理\n",
    "X_train = ss_X.fit_transform(X_train)\n",
    "X_test = ss_X.transform(X_test)\n",
    " \n",
    "#对y做标准化不是必须\n",
    "#对y标准化的好处是不同问题的w差异不太大，同时正则参数的范围也有限\n",
    "y_train = ss_y.fit_transform(y_train.reshape(-1, 1))\n",
    "y_test = ss_y.transform(y_test.reshape(-1, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yr</td>\n",
       "      <td>[1.4286815977773544]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mnth</td>\n",
       "      <td>[0.4274345493572869]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season</td>\n",
       "      <td>[0.3041985312871964]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>atemp</td>\n",
       "      <td>[0.2487796232674795]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>temp</td>\n",
       "      <td>[0.2356916408382094]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>weekday</td>\n",
       "      <td>[0.06956207484487661]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>workingday</td>\n",
       "      <td>[0.045145249940413266]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>holiday</td>\n",
       "      <td>[-0.02917687120063027]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>hum</td>\n",
       "      <td>[-0.05353419121486293]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-0.08916019538030709]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>weathersit</td>\n",
       "      <td>[-0.19855368197656356]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>instant</td>\n",
       "      <td>[-1.0432738159581758]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       columns                    coef\n",
       "2           yr    [1.4286815977773544]\n",
       "3         mnth    [0.4274345493572869]\n",
       "1       season    [0.3041985312871964]\n",
       "9        atemp    [0.2487796232674795]\n",
       "8         temp    [0.2356916408382094]\n",
       "5      weekday   [0.06956207484487661]\n",
       "6   workingday  [0.045145249940413266]\n",
       "4      holiday  [-0.02917687120063027]\n",
       "10         hum  [-0.05353419121486293]\n",
       "11   windspeed  [-0.08916019538030709]\n",
       "7   weathersit  [-0.19855368197656356]\n",
       "0      instant   [-1.0432738159581758]"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    " \n",
    "# 使用默认配置初始化\n",
    "lr = LinearRegression()\n",
    " \n",
    "# 训练模型参数\n",
    "lr.fit(X_train, y_train)\n",
    " \n",
    "# 预测\n",
    "y_test_pred_lr = lr.predict(X_test)\n",
    "y_train_pred_lr = lr.predict(X_train)\n",
    " \n",
    "columns = X.columns\n",
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(columns), \"coef\":list((lr.coef_.T))})\n",
    "fs.sort_values(by=['coef'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LinearRegression on test is 0.823795891798455\n",
      "The r2 score of LinearRegression on train is 0.7944464297551576\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import r2_score  #评价回归预测模型的性能\n",
    "import matplotlib.pyplot as plt\n",
    "# 使用r2_score评价模型在测试集和训练集上的性能，并输出评估结果\n",
    "#测试集\n",
    "print('The r2 score of LinearRegression on test is',r2_score(y_test, y_test_pred_lr))\n",
    "#训练集\n",
    "print('The r2 score of LinearRegression on train is',r2_score(y_train, y_train_pred_lr))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
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117LWWedxWc+8xnGjBnDli1bmDVrFnfffTdHHnnk1rp/9KMf9aiOHTGkJUkA/PnPf2bq1Kk89NBDvPKVr+SNb3wjUAvpW265ZWvAbtq0iVWrVnHcccfxwQ9+kI985COcdNJJHHfccS/Y3p133snMmTMZO7b2hU+nnnoqv/rVr3ZYQ2by0Y9+lNtvv51ddtmFRx55hHXr1rHvvvtuXefoo4/mnHPO4bnnnuOUU05h6tSpPWrv7Nmz2W233dhtt90YN24c69atY8mSJSxfvpyjjz5663sybtw4AK6//nrmz5/P5s2bWbt2Lffff//WkD711FN7VEN3PN0tSQL+ek364Ycf5tlnn916TTozufjii1mxYgUrVqxg9erVzJs3j4MOOojly5dzxBFHcPHFF3PJJZdst82u/gxp+PDhPP/881u3/+yzzwJw7bXXsmHDBpYvX86KFSvYZ599tvtb4+OPP57bb7+d/fffnzPPPJOrr766R+3dbbfdtj4fNmwYmzdvJjOZO3fu1rb+8pe/pK2tjd/85jdcfvnlLFmyhLvvvpvZs2e/oK7dd9+9RzV0x560pF6pdxRyo0crq++MGjWKK664gjlz5nDBBRfwpje9iY9//OOcfvrpjBw5kkceeYSWlhY2b97MmDFjOOOMMxg5ciRXXXXVC7bzqle9igsvvJCNGzey5557csMNNzBlyhSgdh13+fLlvP3tb2fx4sU899xzQO2U+bhx42hpaeG2227j4Ye3/1bHhx9+mP3335/zzjuPp59+mp/97GecddZZDWn7rFmzmDNnDu9///sZN24cf/jDH3jqqad48skn2X333Rk1ahTr1q3j5ptvZubMmQ3Z544Y0pJUoGZ/qJk2bRpTpkzhuuuu48wzz2TlypXMmDEDqA3yuuaaa1i9ejUf+tCH2GWXXWhpaeErX/nKC7Yxfvx42tramDFjBuPHj+eoo47aOgjrvPPOY86cORxzzDHMmjVra0/09NNP5y1veQutra1MnTqVgw8+eLvali5dymWXXUZLSwsjR47ssic9e/bsrffNnjFjBu95z3u6bfehhx7Kpz/9aU444QSef/55WlpauPLKK5k+fTrTpk3jsMMOY/LkyRx77LH1v5m9EJ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cNXt/nlfWfgXz86vEzavfS3hQRbwa+CxzY5Jq2GpAhnZlv2NHyiJgLnATMyuqiwzaKvaVpd22rcxuPVo/rI+JGaqfsignpBrSx2OMHO25fRKyLiPGZubY6Zbi+i220H8MHI2IpMI3atdHS1HMs2tdZExHDgVEMnNOP3bYvMzd2mPwnamNhBpOif996KzOf7PD8XyPiyxGxd2YW8cUig+50d0ScCHwEODkz/9TFaoP2lqYRsXtE7NH+nNpAuk5HNQ5gA/n43QTMrZ7PBbY7cxARL46I3arnewPHAvf3W4U7p55j0bHNbwV+0MWH5xJ1275trs+eDKzsx/r6w03AWdUo7+nAE+2XbAaDiNi3fYxERBxDLRc37vhV/ajZI9ca/QOspnb9ZEX10z6qdD/gXzus92bgV9R6Jx9rdt11tu3vqH2q/QuwDvj3bdtGbRTqL6qf+wZK23amjQP1+FV17wUsAVZVj2Oq+a3A16rnrwbuqY7hPcC8ZtfdTZu2OxbAJdQ+KAOMAG6ofjfvAiY3u+YGt++z1e/aL4DbgIObXfNOtu87wFrguep3bx7wLuBd1fIArqzafw87+MuSEn/qaN97Oxy/nwCvbnbNHX+8LagkSYUadKe7JUkaLAxpSZIKZUhLklQoQ1qSpEIZ0pIkFcqQliSpUIa0JEmF+v9gBPLaW8IZiQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "f, ax = plt.subplots(figsize=(7, 5)) \n",
    "f.tight_layout() \n",
    "ax.hist(y_train - y_train_pred_lr,bins=40, label='Residuals Linear', color='b', alpha=.5); \n",
    "ax.set_title(\"Histogram of Residuals\") \n",
    "ax.legend(loc='best');"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAARgAAADQCAYAAADcQn7hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAA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dwAeODMbSYoON1v4icICI5wOSwp5cC14YeiMhQi8ucJyJFIlJIoLLdR3GGXQpcGloahS2R7I5ncAEjMIZEuZXAkuZdArVeQlwLjAk6Y/8FXGXx+g8J1OlZC7ygqutiDaaqG4D7gH8GK6/Na+F4BhcwYWpDyhGRK4GBqnqD27YYnMXMYAwGg2OYGYzBYHAMM4MxGAyOYQTGYDA4hhEYg8HgGEZgDAaDYxiBMRgMjvH/AX/MhYgSNS1uAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 288x216 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "#还可以观察预测值与真值的散点图\n",
    "plt.figure(figsize=(4, 3))\n",
    "plt.scatter(y_train, y_train_pred_lr)\n",
    "plt.plot([-3, 3], [-3, 3], '--k')   #数据已经标准化，3倍标准差即可\n",
    "plt.axis('tight')\n",
    "plt.xlabel('True price')\n",
    "plt.ylabel('Predicted price')\n",
    "plt.tight_layout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The value of default measurement of SGDRegressor on test is 0.8246375802332985\n",
      "The value of default measurement of SGDRegressor on train is 0.7928176878955053\n"
     ]
    }
   ],
   "source": [
    "# 线性模型，随机梯度下降优化模型参数\n",
    "# 随机梯度下降一般在大数据集上应用\n",
    "from sklearn.linear_model import SGDRegressor\n",
    " \n",
    "# 使用默认配置初始化线\n",
    "sgdr = SGDRegressor(max_iter=1000)\n",
    " \n",
    "# 训练：参数估计\n",
    "sgdr.fit(X_train, y_train.ravel())\n",
    " \n",
    "# 预测\n",
    "#sgdr_y_predict = sgdr.predict(X_test)\n",
    " \n",
    "# 输出给参数的权重\n",
    "sgdr.coef_\n",
    " \n",
    "# 使用SGDRegressor模型自带的评估模块(评价准则为r2_score)，并输出评估结果\n",
    "print('The value of default measurement of SGDRegressor on test is',sgdr.score(X_test, y_test))\n",
    "print('The value of default measurement of SGDRegressor on train is',sgdr.score(X_train, y_train))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of RidgeCV on test is 0.8253843980024096\n",
      "The r2 score of RidgeCV on train is 0.7918487443366922\n",
      "alpha is: 10.0\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yr</td>\n",
       "      <td>[1.4286815977773544]</td>\n",
       "      <td>[0.3926766475807131]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mnth</td>\n",
       "      <td>[0.4274345493572869]</td>\n",
       "      <td>[-0.13856180898595158]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season</td>\n",
       "      <td>[0.3041985312871964]</td>\n",
       "      <td>[0.27830839433475196]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>atemp</td>\n",
       "      <td>[0.2487796232674795]</td>\n",
       "      <td>[0.25130444651087386]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>temp</td>\n",
       "      <td>[0.2356916408382094]</td>\n",
       "      <td>[0.23350772782047863]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>weekday</td>\n",
       "      <td>[0.06956207484487661]</td>\n",
       "      <td>[0.06776464467894688]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>workingday</td>\n",
       "      <td>[0.045145249940413266]</td>\n",
       "      <td>[0.04362904594381467]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>holiday</td>\n",
       "      <td>[-0.02917687120063027]</td>\n",
       "      <td>[-0.029868791152433793]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>hum</td>\n",
       "      <td>[-0.05353419121486293]</td>\n",
       "      <td>[-0.06294137965531557]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-0.08916019538030709]</td>\n",
       "      <td>[-0.09309314839478045]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>weathersit</td>\n",
       "      <td>[-0.19855368197656356]</td>\n",
       "      <td>[-0.18920755339978412]</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>instant</td>\n",
       "      <td>[-1.0432738159581758]</td>\n",
       "      <td>[0.15426842719839406]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       columns                 coef_lr               coef_ridge\n",
       "2           yr    [1.4286815977773544]     [0.3926766475807131]\n",
       "3         mnth    [0.4274345493572869]   [-0.13856180898595158]\n",
       "1       season    [0.3041985312871964]    [0.27830839433475196]\n",
       "9        atemp    [0.2487796232674795]    [0.25130444651087386]\n",
       "8         temp    [0.2356916408382094]    [0.23350772782047863]\n",
       "5      weekday   [0.06956207484487661]    [0.06776464467894688]\n",
       "6   workingday  [0.045145249940413266]    [0.04362904594381467]\n",
       "4      holiday  [-0.02917687120063027]  [-0.029868791152433793]\n",
       "10         hum  [-0.05353419121486293]   [-0.06294137965531557]\n",
       "11   windspeed  [-0.08916019538030709]   [-0.09309314839478045]\n",
       "7   weathersit  [-0.19855368197656356]   [-0.18920755339978412]\n",
       "0      instant   [-1.0432738159581758]    [0.15426842719839406]"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.linear_model import  RidgeCV\n",
    " \n",
    "#设置超参数（正则参数）范围\n",
    "alphas = [ 0.01, 0.1, 1, 10,100]\n",
    " \n",
    "#生成一个RidgeCV实例\n",
    "ridge = RidgeCV(alphas=alphas, store_cv_values=True)  \n",
    " \n",
    "#模型训练\n",
    "ridge.fit(X_train, y_train)    \n",
    " \n",
    "#预测\n",
    "y_test_pred_ridge = ridge.predict(X_test)\n",
    "y_train_pred_ridge = ridge.predict(X_train)\n",
    " \n",
    " \n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print('The r2 score of RidgeCV on test is', r2_score(y_test, y_test_pred_ridge))\n",
    "print('The r2 score of RidgeCV on train is', r2_score(y_train, y_train_pred_ridge))\n",
    " \n",
    "print('alpha is:', ridge.alpha_)\n",
    " \n",
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(columns), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The r2 score of LassoCV on test is 0.8240721992351052\n",
      "The r2 score of LassoCV on train is 0.7908063730989787\n",
      "alpha is: 0.01\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>coef_lasso</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>yr</td>\n",
       "      <td>[1.4286815977773544]</td>\n",
       "      <td>[0.3926766475807131]</td>\n",
       "      <td>0.522253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mnth</td>\n",
       "      <td>[0.4274345493572869]</td>\n",
       "      <td>[-0.13856180898595158]</td>\n",
       "      <td>-0.021166</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season</td>\n",
       "      <td>[0.3041985312871964]</td>\n",
       "      <td>[0.27830839433475196]</td>\n",
       "      <td>0.238309</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>atemp</td>\n",
       "      <td>[0.2487796232674795]</td>\n",
       "      <td>[0.25130444651087386]</td>\n",
       "      <td>0.269177</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>temp</td>\n",
       "      <td>[0.2356916408382094]</td>\n",
       "      <td>[0.23350772782047863]</td>\n",
       "      <td>0.214679</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>weekday</td>\n",
       "      <td>[0.06956207484487661]</td>\n",
       "      <td>[0.06776464467894688]</td>\n",
       "      <td>0.058887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>workingday</td>\n",
       "      <td>[0.045145249940413266]</td>\n",
       "      <td>[0.04362904594381467]</td>\n",
       "      <td>0.037327</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>holiday</td>\n",
       "      <td>[-0.02917687120063027]</td>\n",
       "      <td>[-0.029868791152433793]</td>\n",
       "      <td>-0.025127</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>hum</td>\n",
       "      <td>[-0.05353419121486293]</td>\n",
       "      <td>[-0.06294137965531557]</td>\n",
       "      <td>-0.051614</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>[-0.08916019538030709]</td>\n",
       "      <td>[-0.09309314839478045]</td>\n",
       "      <td>-0.082285</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>weathersit</td>\n",
       "      <td>[-0.19855368197656356]</td>\n",
       "      <td>[-0.18920755339978412]</td>\n",
       "      <td>-0.190071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>instant</td>\n",
       "      <td>[-1.0432738159581758]</td>\n",
       "      <td>[0.15426842719839406]</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       columns                 coef_lr               coef_ridge  coef_lasso\n",
       "2           yr    [1.4286815977773544]     [0.3926766475807131]    0.522253\n",
       "3         mnth    [0.4274345493572869]   [-0.13856180898595158]   -0.021166\n",
       "1       season    [0.3041985312871964]    [0.27830839433475196]    0.238309\n",
       "9        atemp    [0.2487796232674795]    [0.25130444651087386]    0.269177\n",
       "8         temp    [0.2356916408382094]    [0.23350772782047863]    0.214679\n",
       "5      weekday   [0.06956207484487661]    [0.06776464467894688]    0.058887\n",
       "6   workingday  [0.045145249940413266]    [0.04362904594381467]    0.037327\n",
       "4      holiday  [-0.02917687120063027]  [-0.029868791152433793]   -0.025127\n",
       "10         hum  [-0.05353419121486293]   [-0.06294137965531557]   -0.051614\n",
       "11   windspeed  [-0.08916019538030709]   [-0.09309314839478045]   -0.082285\n",
       "7   weathersit  [-0.19855368197656356]   [-0.18920755339978412]   -0.190071\n",
       "0      instant   [-1.0432738159581758]    [0.15426842719839406]    0.000000"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.linear_model import LassoCV\n",
    " \n",
    "#设置超参数搜索范围\n",
    "alphas = [ 0.01, 0.1, 1, 10,100]\n",
    " \n",
    "#生成一个LassoCV实例\n",
    "#lasso = LassoCV(alphas=alphas)  \n",
    "lasso = LassoCV(alphas=alphas)  \n",
    " \n",
    "#训练（内含CV）\n",
    "lasso.fit(X_train, y_train.ravel())  \n",
    " \n",
    "#测试\n",
    "y_test_pred_lasso = lasso.predict(X_test)\n",
    "y_train_pred_lasso = lasso.predict(X_train)\n",
    " \n",
    " \n",
    "# 评估，使用r2_score评价模型在测试集和训练集上的性能\n",
    "print('The r2 score of LassoCV on test is',r2_score(y_test, y_test_pred_lasso))\n",
    "print('The r2 score of LassoCV on train is',r2_score(y_train, y_train_pred_lasso))\n",
    " \n",
    "#打印超参数        \n",
    "print ('alpha is:', lasso.alpha_)\n",
    " \n",
    "# 看看各特征的权重系数，系数的绝对值大小可视为该特征的重要性\n",
    "fs = pd.DataFrame({\"columns\":list(columns), \"coef_lr\":list((lr.coef_.T)), \"coef_ridge\":list((ridge.coef_.T)), \"coef_lasso\":list((lasso.coef_.T))})\n",
    "fs.sort_values(by=['coef_lr'],ascending=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
